<template>
  <div class="syntax-checker">
    <div class="input-section">
      <input
        v-model="filePath"
        type="text"
        class="file-input"
        placeholder="请输入.vue文件路径 (例如: src/**/*.vue)"
      >
      <button
        class="check-button"
        @click="checkSyntax"
        :disabled="isChecking"
      >
        {{ isChecking ? '检查中...' : '开始检查' }}
      </button>
    </div>

    <div v-if="results.length > 0" class="results-section">
      <div class="summary">
        <span class="error-count">错误: {{ totalErrors }} 个</span>
      </div>

      <div
        v-for="(result, index) in results"
        :key="index"
        class="file-result"
      >
        <h3 class="file-path">{{ result.filePath }}</h3>
        <div
          v-for="(message, msgIndex) in filteredMessages(result.messages)"
          :key="msgIndex"
          class="message error"
        >
          <div class="message-header">
            <span class="severity">错误</span>
            <span class="rule">[{{ message.ruleId || '语法错误' }}]</span>
          </div>
          <div class="message-location">
            位置: 第 {{ message.line || '未知' }} 行, 第 {{ message.column || '未知' }} 列
          </div>
          <div class="message-content">
            问题: {{ message.message }}
          </div>
          <div v-if="message.fix" class="message-fix">
            建议: {{ message.fix.text }}
          </div>
        </div>
      </div>
    </div>

    <div v-else-if="checked" class="success-message">
      ✓ 代码检查通过，未发现问题！
    </div>
  </div>
</template>

<script>
  export default {
    name: 'SyntaxChecker',
    data() {
      return {
        filePath: '',
        isChecking: false,
        checked: false,
        results: [],
        totalErrors: 0
      };
    },
    methods: {
      filteredMessages(messages) {
        return messages.filter(msg => msg.severity === 2);
      },
      async checkSyntax() {
        if (!this.filePath) {
          alert('请输入文件路径');
          return;
        }

        this.isChecking = true;
        this.checked = false;
        this.results = [];

        try {
          const response = await fetch('/api/check', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify({
              pattern: this.filePath
            })
          });

          if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
          }

          const data = await response.json();
          
          if (data.success) {
            this.results = data.results;
            this.totalErrors = data.errorCount;
            this.checked = true;
          } else {
            throw new Error(data.error || '检查过程中发生未知错误');
          }
        } catch (error) {
          console.error('Error:', error);
          alert('请求失败：' + error.message);
        } finally {
          this.isChecking = false;
        }
      }
    }
  };
</script>

<style>
.syntax-checker {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

.input-section {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.file-input {
  flex: 1;
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.check-button {
  padding: 8px 20px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.check-button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.summary {
  margin-bottom: 20px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.error-count {
  color: #d32f2f;
}

.file-result {
  margin-bottom: 30px;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 15px;
}

.file-path {
  margin: 0 0 15px 0;
  color: #2196F3;
  font-size: 16px;
}

.message {
  margin-bottom: 15px;
  padding: 10px;
  border-radius: 4px;
}

.message.error {
  background-color: #ffebee;
  border-left: 4px solid #d32f2f;
}

.message-header {
  margin-bottom: 8px;
}

.severity {
  font-weight: bold;
  margin-right: 10px;
}

.rule {
  color: #666;
  font-size: 0.9em;
}

.message-location,
.message-content,
.message-fix {
  margin: 5px 0;
  font-size: 14px;
}

.message-fix {
  color: #2196F3;
}

.success-message {
  color: #4CAF50;
  font-size: 16px;
  text-align: center;
  padding: 20px;
  background-color: #E8F5E9;
  border-radius: 4px;
}
</style>